package implementation;

import GameData.Game;
import GameData.Games;
import protocol.Query;
import protocol.State;
import protocol.StateObject;

public class AwayTeamConfHandler {

	public static void handleAwayTeamConf(StateObject nextState, Query query) {
		String standardPrompt;
		
		/**
		 * The user did not explicitly choose an away team
		 */
		if(query.getAwayTeam().equals("any") || query.getAwayTeam().equals("any team")){
			//Get the next available home game
			if(query.getDate() == null || query.getDate().equals("next")){
				Game game = Games.searchNextHomeTeam(query.getHomeTeam());
				query.setGameInfo(game);
				//NoDateAny
				standardPrompt = "NextGameHomeTeam%" + query.getHomeTeam() + "," 
									+ game.awayTeam + "," + game.getDate() + "," 
									+ game.time;
			}
			//Get the home game based off the user's date
			else{
				Game game = Games.searchByHomeTeam(query.getHomeTeam(), query.getDate());
				query.setGameInfo(game);
				if(game.getDate().equals(query.getDate())) {
					//MatchDateAny
					standardPrompt = "GameOnThisDate%" + game.getDate() + "," + query.getHomeTeam() + "," 
										+ game.awayTeam	+ "," + game.time;
				}
				else {
					//NoMatchDateAny
					standardPrompt = "NoHomeTeamGameOnDate%" + query.getHomeTeam() + "," 
										+ game.awayTeam + "," + query.getDate() + ","
										+ game.getDate() + "," + game.time;
				}
			}
		}
		
		/**
		 * The user explicitly chose an away team
		 */
		else{
			//get the next available home + away game
			if(query.getDate() == null || query.getDate().split(" ")[0].equals("next")){
				Game game = Games.searchNextHomeAwayTeam(query.getHomeTeam(), query.getAwayTeam());
				query.setGameInfo(game);
				if(game == null)
				{
					nextState.setState(State.HOMETEAM);
					standardPrompt = "NoMatchGame";
				}
				else
				{
					query.setGameInfo(game);
					//NoDateTeam
					standardPrompt = "NextGameHomeAwayTeam%" + query.getHomeTeam() + "," 
										+ game.awayTeam + "," + game.getDate() + "," 
										+ game.time;
				}
			}
			//Get the home + away game based off the user's date 
			else{
				Game game = Games.searchByHomeAwayTeam(query.getHomeTeam(), query.getAwayTeam(), query.getDate());
				query.setGameInfo(game);
				if(game.getDate().equals(query.getDate())){
					//MatchDateTeam
					standardPrompt = "HomeTeamAwayTeamGameOnDate%" + game.getDate() + "," + query.getHomeTeam() + "," 
										+ game.awayTeam	+ "," + game.time;
				}
				else{
					//NoMatchDateTeam
					standardPrompt = "NoHomeTeamAwayTeamGameOnDate%" + query.getHomeTeam() + "," 
										+ game.awayTeam + "," + query.getDate() + ","
										+ game.getDate() + "," + game.time;
				}
			}
		}
		ExecUtils.playPrompt(standardPrompt, nextState);
		if( standardPrompt.equals("NoMatchGame") ){
			nextState.setState(State.AWAYTEAM);
			return;
		}

		String helpPrompt = "AwayTeamConfirmationHelp";
		boolean receivedInput = false;
		while (!receivedInput) {
			
			ExecUtils.getUserInput(query, "gram_awayteamconf");
			//user provided a directive
			if (query.getShortConf() != null) {
				String response = query.getShortConf().trim().toLowerCase();
				
				if (response.equals("assistance")) {
					ExecUtils.playPrompt(helpPrompt, nextState);
				} 
				else if (response.equals("goback") || response.equals("go back")) {
					ExecUtils.playPrompt(response, nextState);
					receivedInput = true;
					nextState.setState(State.AWAYTEAM);
				}
				else if(response.equals("start over") || response.equals("startover")) {
					ExecUtils.playPrompt(response, nextState);
					receivedInput = true;
					nextState.setState(State.NUMTIX);
				}
				else if(response.equals("yes") || response.equals("correct") || response.equals("that's correct")){
					receivedInput = true;
					nextState.setState(State.PRICE);
				}
				else if(response.equals("no")){
					receivedInput = true;
					nextState.setState(State.DATE);
				}
				query.voidTransaction();
			}
			else {
				query.voidTransaction();
				ExecUtils.playPrompt(helpPrompt, nextState);
			}
		}
	}
}
